package com.bocai.yuweiwangluo.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.bean.VenueListBean;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.UrlData;

import java.text.DecimalFormat;


public class VenueListAdapter extends BaseAdapter
{
    private Context context;
    private VenueListBean venueListBean;
    public VenueListAdapter(Context context,VenueListBean venueListBean)
    {
        this.context=context;
        this.venueListBean=venueListBean;
    }
    public void addData(VenueListBean bean){
        venueListBean.getData().addAll(bean.getData());
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount()
    {
        return venueListBean.getData().size();
    }

    @Override
    public Object getItem(int position)
    {
        return venueListBean.getData().get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_venue_list,null);
            viewHolder.ivVenueImg=(ImageView)convertView.findViewById(R.id.iv_venue_img);
            viewHolder.tvVenueName=(TextView)convertView.findViewById(R.id.tv_venue_name);
            viewHolder.tvVenueDistance=(TextView)convertView.findViewById(R.id.tv_venue_distance);
            viewHolder.tvVenueIntroduce=(TextView)convertView.findViewById(R.id.tv_venue_introduce);
            viewHolder.tvYuweiMoney=(TextView)convertView.findViewById(R.id.tv_venue_yuwei_money);
            viewHolder.tvVenueMoney=(TextView)convertView.findViewById(R.id.tv_venue_money);
            viewHolder.tvVenueMoney.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );//设置中间横线
            viewHolder.tvVenueSoldCount=(TextView)convertView.findViewById(R.id.tv_venue_soldcount);
            convertView.setTag(viewHolder);
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        VenueListBean.Data data=venueListBean.getData().get(position);
        ImageLoaderUtil.loaderImageDefault(UrlData.IMAGEURL + data.getPic(), viewHolder.ivVenueImg);
        viewHolder.tvVenueName.setText(data.getTitle());
        viewHolder.tvVenueSoldCount.setText("已售:"+data.getSale_num());
        viewHolder.tvVenueMoney.setText("原价"+Math.round(Double.parseDouble(data.getPrice()))+"元");
        viewHolder.tvYuweiMoney.setText(Math.round(Double.parseDouble(data.getItem_price()))+"");
        viewHolder.tvVenueIntroduce.setText(data.getContent());
        DecimalFormat df = new DecimalFormat("#0.0");
        viewHolder.tvVenueDistance.setText(df.format(Double.parseDouble(data.getMin_local()))+"km");

        return convertView;
    }
    public class ViewHolder
    {
        ImageView ivVenueImg;//场馆图片
        TextView tvVenueName;//场馆名
        TextView tvVenueDistance;//场馆距离
        TextView tvVenueIntroduce;//场馆介绍
        TextView tvVenueMoney;//场馆价格
        TextView tvVenueSoldCount;//场馆售出数
        TextView tvYuweiMoney;
    }
}
